package com.uphone.edwards.pro.activity.login;

import android.arch.lifecycle.LifecycleOwner;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.radish.baselibrary.utils.EncryptUtils;
import com.radish.baselibrary.utils.LogUtils;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.edwards.MyApp;
import com.uphone.edwards.base.mvp.BasePAV;
import com.uphone.edwards.http.ApiService;
import com.uphone.edwards.http.RxSchedulers;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by hzy on 2019/1/18
 * LoginPresenter
 *
 * @author hzy
 *
 */
public class LoginPresenter extends BasePAV<LoginContract.View> implements LoginContract.Presenter {

    @Inject
    LoginPresenter() {

    }

    @Override
    public void postLogin(String email, String code) {
        mView.showLoading();

        MyApp.apiService(ApiService.class)
                .login(MyApp.languageType , email, code)
                .compose(RxSchedulers.io_main())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(bean -> {
                    mView.closeLoading();
                    if (bean != null) {
                        try {
                            String add = bean.string();

                            JSONObject jsonObject = new JSONObject(add);
                            int status = jsonObject.getInt("code");
                            if (status != 1) {
                                ToastUtils.showShort(jsonObject.getString("msg"));
                            }
                            mView.updateView(add , status);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, throwable -> {
                    mView.closeLoading();
                    mView.onFail();
                });
    }

    @Override
    public void postLoginCode(String email) {
        mView.showLoading();
        Map<String, String> map = new HashMap<>();
        MyApp.apiService(ApiService.class)
                .sendLoginEmail(MyApp.languageType , email)
                .compose(RxSchedulers.io_main())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(bean -> {
                    mView.closeLoading();
                    if (bean != null) {
                        try {
                            String add = bean.string();

                            JSONObject jsonObject = new JSONObject(add);
                            int status = jsonObject.getInt("code");
                            if (status != 1) {
                                ToastUtils.showShort(jsonObject.getString("msg"));
                            }
                            mView.loginCodeView(add , status);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, throwable -> {
                    mView.closeLoading();
                    mView.onFail();
                });
    }
}
